package com.itcast.donkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.adapters.ScenicAttackAdapter;
import com.itcast.donkey.adapters.TravelNotesAdapter;
import com.itcast.donkey.modles.NotesModles;
import com.itcast.donkey.modles.ScenicAttackModles;
import com.itcast.donkey.modles.UriModles;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 攻略界面点击之后，一个点击按钮
 * Created by lanouhn on 16/8/25.
 */
public class ScenicAttackActivity extends AppCompatActivity implements View.OnClickListener {


    private ImageView backScenicApot;
    private ImageView ivChanScenicSpot;
    private TextView tvTitleScenicSpot;
    private ImageView ivShareJourney;
    private RecyclerView recyclerSpotAttack;
    private String uri = null;
    private boolean Wiki_app_publish = true;

    //当是游记的时候调用
    private ArrayList<NotesModles> notesModlesList;
    //当是攻略时候调用
    private ArrayList<ScenicAttackModles> modlesList;

    //攻略的适配器
    private ScenicAttackAdapter adapter;

    //游记的适配器
    private TravelNotesAdapter notesAdapter;
    private String result = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scenic_attack);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        backScenicApot = (ImageView) findViewById(R.id.back_scenic_apot);
        ivChanScenicSpot = (ImageView) findViewById(R.id.iv_chan_scenic_spot);
        tvTitleScenicSpot = (TextView) findViewById(R.id.tv_title_scenic_spot);
        ivShareJourney = (ImageView) findViewById(R.id.iv_share_journey);
        recyclerSpotAttack = (RecyclerView) findViewById(R.id.recycler_spot_attack);
    }

    private void initData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        tvTitleScenicSpot.setText(name + "攻略");

        Wiki_app_publish = intent.getBooleanExtra("Wiki_app_publish", false);
        int id = intent.getIntExtra("id", 0);

        if (Wiki_app_publish) {
            uri = UriModles.ATTACK_CLICK_FRONT_SCENIC_ATTACK_URL + id + UriModles.ATTACK_CLICK_END_SCENIC_ATTACK_URL;
        } else {
            uri = UriModles.ATTACK_CLICK_FRONT_TRAVEL_NOTES_URL + id + UriModles.ATTACK_CLICK_END_TRAVEL_NOTES_URL;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(uri)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        result = response.body().string();
                        Gson gson = new Gson();
                        modlesList = new ArrayList<>();
                        notesModlesList = new ArrayList<NotesModles>();
                        try {

                            JSONArray array = new JSONArray(result);
                            for (int i = 0; i < array.length(); i++) {
                                if (Wiki_app_publish) {
                                    ScenicAttackModles modles = gson.fromJson(array.get(i).toString(), ScenicAttackModles.class);
                                    modlesList.add(modles);
                                } else {
                                    NotesModles notesModles = gson.fromJson(array.get(i).toString(), NotesModles.class);
                                    notesModlesList.add(notesModles);
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        if (modlesList.size() > 0 || notesModlesList.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(ScenicAttackActivity.this, "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    //设置设配器
                    if (Wiki_app_publish) {
                        adapter = new ScenicAttackAdapter(ScenicAttackActivity.this, modlesList, result);
                        recyclerSpotAttack.setAdapter(adapter);

                    } else {
                        notesAdapter = new TravelNotesAdapter(ScenicAttackActivity.this, notesModlesList);
                        recyclerSpotAttack.setAdapter(notesAdapter);
                    }
                    LinearLayoutManager lineManager = new LinearLayoutManager(ScenicAttackActivity.this);
                    recyclerSpotAttack.setLayoutManager(lineManager);
                    break;
                case 2:
                    Toast.makeText(ScenicAttackActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private void initEvent() {
        backScenicApot.setOnClickListener(this);
        ivChanScenicSpot.setOnClickListener(this);
        ivShareJourney.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_scenic_apot:
            case R.id.iv_chan_scenic_spot:
                finish();
                break;
            case R.id.iv_share_journey:

                break;
        }
    }
}
